﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using mEdit.Properties;

namespace mEdit
{
    public partial class mEditMainForm : Form
    {
        public mEditMainForm()
        {
            InitializeComponent();
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.editor.CutSelectedTextIntoClipboard();
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.editor.CopySelectedTextIntoClipboard();
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.editor.PasteFromClipboard();
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void tsmiAbout_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.ShowDialog(this);
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            bool show = this.WindowState != FormWindowState.Minimized;
            if (show)
            {
                Form f = this.ActiveMdiChild;
                if (f != null)
                    f.WindowState = FormWindowState.Maximized;
            }
        }

        private void tsmiClose_Click(object sender, EventArgs e)
        {
            this.MainForm_FormClosing(sender, new FormClosingEventArgs(CloseReason.UserClosing, false));
        }

        private void tsmiHideRestore_Click(object sender, EventArgs e)
        {
            switch (this.WindowState)
            {
                case FormWindowState.Minimized:
                    this.WindowState = FormWindowState.Normal;
                    break;
                case FormWindowState.Maximized:
                    this.WindowState = FormWindowState.Normal;
                    break;
                case FormWindowState.Normal:
                    this.WindowState = FormWindowState.Minimized;
                    break;
            }
        }

        private void ntfIcon_Click(object sender, System.EventArgs e)
        {
            tsmiHideRestore_Click(this, EventArgs.Empty);
        }

        private void tsmiOptions_Click(object sender, EventArgs e)
        {
            Options options = new Options();
            options.CharSet = ScintillaNet.CharacterSet.EastEurope;
            options.EncodingName = Settings.Default.Encoding;

            if (options.ShowDialog(this) == DialogResult.OK)
            {
                this.treeTextEditor.ChangeEditorOutlook(options);
                this.StoreProperties(options);
            }
        }

        private void StoreProperties(Options options)
        {
            Properties.Settings.Default.EditorForeColor = options.EditorForeColor;
            Properties.Settings.Default.EditorBackColor = options.EditorBackColor;
            Properties.Settings.Default.LineBackColor = options.LineBackColor;
            Properties.Settings.Default.LineForeColor = options.LineForeColor;
            Properties.Settings.Default.EditorFont = options.EditorFont;
            //this.editor
            Properties.Settings.Default.GDICharSet = options.EditorFont.GdiCharSet;
            // encoding
            Properties.Settings.Default.Encoding = options.EncodingName;
            Properties.Settings.Default.Save();
        }

        private void SaveOptions()
        {
            Properties.Settings.Default.WindowSize = this.Size;
            Properties.Settings.Default.WindowPostion = this.Location;
            Settings.Default.MarginWidth = this.treeTextEditor.LineNumberMarginWidth;
            Properties.Settings.Default.Save();
        }

        private void LoadOptions()
        {
            this.Location = Properties.Settings.Default.WindowPostion != new Point(-1, -1) ? Properties.Settings.Default.WindowPostion : this.Location;
            this.Size = Properties.Settings.Default.WindowSize != new Size(-1, -1) ? Properties.Settings.Default.WindowSize : this.Size;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.LoadOptions();
            bool fileOpened = false;
            if (!string.IsNullOrEmpty(Properties.Settings.Default.LastOpenedFile))
            {
                if (File.Exists(Properties.Settings.Default.LastOpenedFile))
                {
                    this.treeTextEditor.OpenFile(Properties.Settings.Default.LastOpenedFile);
                    fileOpened = true;
                }
                else
                {
                    string msg = string.Format("{0}{1}{2}", "Recently edited file has been moved or deleted...", System.Environment.NewLine, "No file will be opened.");
                    MessageBox.Show(this, msg, "Could not open file", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Properties.Settings.Default.LastOpenedFile = string.Empty;
                }
            }
            // if no file was opened, set the default title from resources
            if (!fileOpened)
            {
                this.Text = Resources.AppTitle;
                this.treeTextEditor.CreateNew();
            }
            CreateMRUMenu();
        }

        private void CreateMRUMenu()
        {
            MRU.CreateMRUMenu();
        }

        private void ClearMRUMenu()
        {
            MRU.ClearMRUMenu();
        }

        private void tsmiMRU_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem tsmi = (ToolStripMenuItem)sender;
            OpenFile(tsmi.Text);
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.SaveOptions();
            if (!this.treeTextEditor.IsContentChanged)
                return;
            DialogResult dr = MessageBox.Show("Content changed. Do you want to save or lose changes?", "Warning", 
                MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                this.treeTextEditor.SaveContent();
                if (this.treeTextEditor.IsContentChanged)
                    e.Cancel = true;
            }
            else if (dr == DialogResult.Cancel)
            {
                e.Cancel = true;
            }
        }

        private void tsmiShortcutKeys_Click(object sender, EventArgs e)
        {
            ShortcutKeys keys = new ShortcutKeys();
            keys.ShowDialog(this);
        }

        private void tsmiSaveAs_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = Resources.AppFileFilter;
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                this.treeTextEditor.Save(saveFileDialog.FileName);
                Settings.Default.LastOpenedFile = saveFileDialog.FileName;
                Settings.Default.Save();
            }
        }

        private void tsmiOpenFile_Click(object sender, EventArgs e)
        {
            if (!CheckAndSaveContent())
                return;
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = Resources.AppFileFilter;
            if (!string.IsNullOrEmpty(Properties.Settings.Default.LastDirectory))
                openFileDialog.InitialDirectory = Properties.Settings.Default.LastDirectory;
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                OpenFile(openFileDialog.FileName);
            }
        }

        private void OpenFile(string fileName)
        {
            this.treeTextEditor.OpenFile(fileName);
        }

        private bool CheckAndSaveContent()
        {
            if (this.treeTextEditor.IsContentChanged)
            {
                DialogResult dr = MessageBox.Show("[main] Content is not saved! Save?", "Do you want...", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                    treeTextEditor.SaveContent();
                if (dr == DialogResult.Cancel)
                    return false;
            }
            return true;
        }

        private void tsmiNew_Click(object sender, EventArgs e)
        {
            if (!CheckAndSaveContent())
                return;
            treeTextEditor.CreateNew();
            Settings.Default.LastOpenedFile = string.Empty;
        }

        private void treeTextEditor_CurrentFileChanged(string fileName)
        {
            if (!string.IsNullOrEmpty(fileName))
                this.Text = String.Format(Resources.AppTitleFormat, Resources.AppTitle, fileName);
            else
                this.Text = Resources.AppTitle;
        }
    }
}